import { QrcodeconfigService } from './../../../../@core/services/QRCodeConfig/qrcodeconfig.service';
import { Component, Input } from '@angular/core';
import { QRCodeConfig } from 'src/app/@core/data/QRCodeConfig';

@Component({
  selector: 'app-qrcode-config-add',
  templateUrl: './qrcode-config-add.component.html',
  styleUrls: ['./qrcode-config-add.component.scss'],
})
export class QRCodeConfigAddComponent {
  @Input() data: any;
  constructor(private QrcodeconfigService:QrcodeconfigService) {}
  halfCheck = false;
  allCheck = false;
  options = [

  ];
  QRCodeConfig: QRCodeConfig = {
    qrCodeConfigurationName: '',
    isDefault: false,
    orderType: '采购到货二维码配置',
    configurations: [],
    description: '',
  };
  ngOnInit(): void {
  this.QrcodeconfigService.getOrderCodeConfigurationEnum(this.data.type).subscribe((res: any) => {
    this.options = res;
    this.QRCodeConfig.orderType = this.data.type;
  });
  if (this.data.rowItem) {
    // 深拷贝 this.data.rowItem
    this.QRCodeConfig = JSON.parse(JSON.stringify(this.data.rowItem));
    if (
      this.QRCodeConfig.configurations.length > 0 &&
      this.QRCodeConfig.configurations.length < this.options.length
    ) {
      this.halfCheck = true;
      this.allCheck = false;
    } else if (this.QRCodeConfig.configurations.length === 0) {
      this.allCheck = false;
      this.halfCheck = false;
    } else if (
      this.QRCodeConfig.configurations.length === this.options.length
    ) {
      this.halfCheck = false;
      this.allCheck = true;
    }
  }
  }
  checkGroup(event: any) {
    if (
      this.QRCodeConfig.configurations.length > 0 &&
      this.QRCodeConfig.configurations.length < this.options.length
    ) {
      this.halfCheck = true;
      this.allCheck = false;
    } else if (this.QRCodeConfig.configurations.length === 0) {
      this.allCheck = false;
      this.halfCheck = false;
    } else if (
      this.QRCodeConfig.configurations.length === this.options.length
    ){
      this.halfCheck = false;
      this.allCheck = true;
    }
  }
  
  allCheckChange(event: any) {
    this.allCheck = event;
    this.halfCheck = false;
    if (this.allCheck) {
      this.QRCodeConfig.configurations = this.options;
    } else {
      this.QRCodeConfig.configurations = [];
    }
  }
}
